package com.msconfig.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;

/*
 * author: caojunhao
 * date  : 2020/5/14
 * desc  :绘制主页下面的banner
 */
public class ViewBottomNavigation extends RelativeLayout implements View.OnClickListener{
    private LinearLayout llSatellite;
    private LinearLayout llEquipment;
    private LinearLayout llSetup;
    private LinearLayout llOther;
    private ImageView ivSatelliteOn;
    private ImageView ivSatelliteOff;
    private TextView tvSatellite;

    private ImageView ivEquipmentOn;
    private ImageView ivEquipmentOff;
    private TextView tvEquipment;

    private ImageView ivSetupOn;
    private ImageView ivSetupOff;
    private TextView tvSetup;

    private ImageView ivOtherOn;
    private ImageView ivOtherOff;
    private TextView tvOther;

    private ITabChangeListener mITabChangeListener;
    private int mIndex = -1;
    private int mPrevious = -1;



    public ViewBottomNavigation(Context context) {
        super(context);
        init(context,this);
    }

    public ViewBottomNavigation(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,this);
    }

    public ViewBottomNavigation(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,this);
    }

    private void init(Context context, ViewGroup viewGroup) {
        View view = View.inflate(context, R.layout.layout_view_bottom_navigation, viewGroup);
        llSatellite=view.findViewById(R.id.ll_satellite);
        llEquipment=view.findViewById(R.id.ll_equipment);
        llSetup=view.findViewById(R.id.ll_setup);
        llOther=view.findViewById(R.id.ll_other);

        ivSatelliteOn=view.findViewById(R.id.iv_bottom_satellite_on);
        ivSatelliteOff=view.findViewById(R.id.iv_bottom_satellite_off);
        tvSatellite=view.findViewById(R.id.tv_bottom_satellite);

        ivEquipmentOn=view.findViewById(R.id.iv_bottom_equipment_on);
        ivEquipmentOff=view.findViewById(R.id.iv_bottom_equipment_off);
        tvEquipment=view.findViewById(R.id.tv_bottom_equipment);

        ivSetupOn=view.findViewById(R.id.iv_bottom_setup_on);
        ivSetupOff=view.findViewById(R.id.iv_bottom_setup_off);
        tvSetup=view.findViewById(R.id.tv_bottom_setup);

        ivOtherOn=view.findViewById(R.id.iv_bottom_other_on);
        ivOtherOff=view.findViewById(R.id.iv_bottom_other_off);
        tvOther=view.findViewById(R.id.tv_bottom_other);


        llSatellite.setOnClickListener(this);
        llEquipment.setOnClickListener(this);
        llSetup.setOnClickListener(this);
        llOther.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        //过滤自己
        if (viewId == R.id.ll_satellite && mIndex == 0) {
            return;
        }
        if (viewId == R.id.ll_equipment && mIndex == 1) {
            return;
        }
        if (viewId == R.id.ll_setup && mIndex == 2) {
            return;
        }
        if (viewId == R.id.ll_other && mIndex == 3) {
            return;
        }

        switch (viewId) {
            case R.id.ll_satellite:
                tvSatellite.setTextColor(0xFF519AF8);
                ivSatelliteOff.setVisibility(GONE);
                ivSatelliteOn.setVisibility(VISIBLE);
                mPrevious=mIndex;
                mIndex=0;
                break;
            case R.id.ll_equipment:
                tvEquipment.setTextColor(0xFF519AF8);
                ivEquipmentOff.setVisibility(GONE);
                ivEquipmentOn.setVisibility(VISIBLE);
                mPrevious=mIndex;
                mIndex=1;
                break;

            case R.id.ll_setup:
                tvSetup.setTextColor(0xFF519AF8);
                ivSetupOff.setVisibility(GONE);
                ivSetupOn.setVisibility(VISIBLE);
                mPrevious=mIndex;
                mIndex=2;
                break;
            case R.id.ll_other:
                tvOther.setTextColor(0xFF519AF8);
                ivOtherOff.setVisibility(GONE);
                ivOtherOn.setVisibility(VISIBLE);
                mPrevious=mIndex;
                mIndex=3;
                break;
        }
        changePreviousState();
        if (mITabChangeListener != null) {
            //外部如果设置了点击事件，这里回传
            mITabChangeListener.onTabSelected(mIndex);
        }
    }

    private void changePreviousState() {
        if (mPrevious == 0) {
            tvSatellite.setTextColor(0xFF000000);
            ivSatelliteOff.setVisibility(VISIBLE);
            ivSatelliteOn.setVisibility(GONE);
        }else if(mPrevious==1){
            tvEquipment.setTextColor(0xFF000000);
            ivEquipmentOff.setVisibility(VISIBLE);
            ivEquipmentOn.setVisibility(GONE);
        }else if(mPrevious==2){
            tvSetup.setTextColor(0xFF000000);
            ivSetupOff.setVisibility(VISIBLE);
            ivSetupOn.setVisibility(GONE);
        }else if(mPrevious==3){
            tvOther.setTextColor(0xFF000000);
            ivOtherOff.setVisibility(VISIBLE);
            ivOtherOn.setVisibility(GONE);
        }
    }


    public void setTabChangeListener(ITabChangeListener iTabChangeListener) {
        this.mITabChangeListener = iTabChangeListener;
    }

    public interface ITabChangeListener {
        void onTabSelected(int index);
    }

    /**
     * 外部可以主动这是某个tab位置。
     *
     * @param index 具体位置
     */
    public void setCurrentTab(int index) {
        if (index < 0 || index > 2) {
            throw new IllegalArgumentException("out of mIndex");
        }
        switch (index) {
            case 0:
                onClick(llSatellite);
                break;
            case 1:
                onClick(llEquipment);
                break;
            case 2:
                onClick(llSetup);
                break;
            case 3:
                onClick(llOther);
                break;
        }
    }

}
